package edu.uc.ui;

import java.util.ArrayList;

import edu.uc.dao.IUserDAO;
import edu.uc.dao.UserProviderStub;
import edu.uc.dto.User;
import edu.uc.service.IUserService;
import edu.uc.service.UserServiceStub;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 
 * @author ckattick
 * Displays the create new user screen, handles simple validation, and handles component listeners
 */
public class CreateNewUserScreen extends CeasBaseScreen {

	// declare local variables
	private EditText newUsername;
	private EditText newPassword;
	private EditText firstName;
	private EditText lastName;
	private EditText phone;
	private Button newUser;
	private Spinner prefix;
	private String prefixValue;
	private IUserDAO userDAO;
	private IUserService userService;
	private TextView warning;
	private Button login;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newuser);
		initComponents();
	}

	private void initComponents() {
		// initialize UserProvider
		userDAO = new UserProviderStub();

		// set local to component id objects
		newUsername = (EditText) findViewById(R.id.inputusername);
		newPassword = (EditText) findViewById(R.id.inputpassword);
		firstName = (EditText) findViewById(R.id.inputfirstname);
		lastName = (EditText) findViewById(R.id.inputlastname);
		phone = (EditText) findViewById(R.id.inputphonenum);
		newUser = (Button) findViewById(R.id.btncreateuser);
		warning = (TextView) findViewById(R.id.warning1);
		login = (Button) findViewById(R.id.login1);

		// spinner stuff
		prefix = (Spinner) findViewById(R.id.prefix);

		ArrayList<String> allPrefix = userDAO.fetchAllPrefix();

		ArrayAdapter<String> prefixAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, allPrefix);

		// finalize the values that go into the spinner
		prefix.setAdapter(prefixAdapter);

		// declare create user button click event
		newUser.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {

				createUser();
			}
		});
		login.setOnClickListener(new OnClickListener() {

			// @Override
			public void onClick(View v) {
				loadLoginScreen();
			}
		});
	}

	public void createUser() {
		if (newUsername.getText().toString().length() == 0) {
			newUsername.setError("Invalid Username");
		}
		if (newPassword.getText().toString().length() == 0) {
			newPassword.setError("Invalid Password");
		}
		if (firstName.getText().toString().length() == 0) {
			firstName.setError("Please enter a name");
		}
		if (lastName.getText().toString().length() == 0) {
			lastName.setError("Please enter a last name");
		}
		if (phone.getText().toString().length() == 0) {
			phone.setError("Please enter a phone number");
		} else {
			try {
				userService = new UserServiceStub();
				prefixValue = prefix.getSelectedItem().toString();
				User newUser = new User();

				newUser.setUsername(newUsername.getText().toString());
				newUser.setPassword(newPassword.getText().toString());
				newUser.setPrefix(prefixValue);
				newUser.setFirstName(firstName.getText().toString());
				newUser.setLastName(lastName.getText().toString());
				newUser.setPhoneNumber(phone.getText().toString());

				userService.save(newUser);
				warning.setText("User Created, please go to the login screen");
				login.setVisibility(0);

			} catch (Exception e) {
				warning.setText("Unable to save user");
			}
		}

	}

	private void loadLoginScreen() {
		Intent LoginUser = new Intent(this, IntroScreen.class);
		startActivity(LoginUser);

	}
}
